import { Dimensions, Platform, StatusBar, PixelRatio, ScaledSize } from 'react-native';

// 获取屏幕尺寸
const { width: SCREEN_WIDTH, height: SCREEN_HEIGHT } = Dimensions.get('window');

// 设计稿尺寸（以 iPhone 6/7/8 为基准）
const DESIGN_WIDTH = 375;
const DESIGN_HEIGHT = 667;

// 计算缩放比例
const scale = SCREEN_WIDTH / DESIGN_WIDTH;

// 状态栏高度
const STATUS_BAR_HEIGHT = Platform.OS === 'ios' ? 44 : StatusBar.currentHeight ?? 0;

// 底部安全区域高度
const BOTTOM_SAFE_HEIGHT = Platform.OS === 'ios' ? 34 : 0;

// 获取设备像素比
const pixelRatio = PixelRatio.get();

// 是否是刘海屏
const isNotchScreen = Platform.OS === 'ios'
  ? (SCREEN_HEIGHT === 812 || SCREEN_HEIGHT === 896 || SCREEN_HEIGHT === 844 || SCREEN_HEIGHT === 926)
  : (StatusBar.currentHeight ?? 0) > 24;

// 屏幕尺寸分类
const ScreenSize = {
  small: SCREEN_WIDTH < 375,
  medium: SCREEN_WIDTH >= 375 && SCREEN_WIDTH <= 414,
  large: SCREEN_WIDTH > 414,
};

// 设备类型
const DeviceType = {
  isIOS: Platform.OS === 'ios',
  isAndroid: Platform.OS === 'android',
  isNotchScreen,
  platform: Platform.OS,
  ...ScreenSize,
};

// 适配尺寸（根据屏幕宽度等比例缩放）
export const adapt = (size: number): number => {
  return Math.round(size * scale);
};

// 适配字体大小（考虑设备像素比）
export const adaptFontSize = (size: number): number => {
  const scaledSize = size * scale;
  return Math.round(scaledSize * pixelRatio) / pixelRatio;
};

// 适配高度（根据屏幕高度等比例缩放）
export const adaptHeight = (size: number): number => {
  const heightScale = SCREEN_HEIGHT / DESIGN_HEIGHT;
  return Math.round(size * heightScale);
};

// 适配边距（根据屏幕宽度等比例缩放，但设置最小和最大值）
export const adaptMargin = (size: number, min: number = 0, max: number = Infinity): number => {
  const adaptedSize = adapt(size);
  return Math.min(Math.max(adaptedSize, min), max);
};

// 适配圆角（根据屏幕宽度等比例缩放，但设置最小和最大值）
export const adaptRadius = (size: number, min: number = 0, max: number = Infinity): number => {
  const adaptedSize = adapt(size);
  return Math.min(Math.max(adaptedSize, min), max);
};

// 获取屏幕宽度
export const getScreenWidth = (): number => {
  return SCREEN_WIDTH;
};

// 获取屏幕高度
export const getScreenHeight = (): number => {
  return SCREEN_HEIGHT;
};

// 获取状态栏高度
export const getStatusBarHeight = (): number => {
  return STATUS_BAR_HEIGHT;
};

// 获取底部安全区域高度
export const getBottomSafeHeight = (): number => {
  return isNotchScreen ? BOTTOM_SAFE_HEIGHT : 0;
};

// 获取导航栏高度
export const getNavBarHeight = (): number => {
  return STATUS_BAR_HEIGHT + 44;
};

// 获取屏幕可用高度（除去状态栏和底部安全区域）
export const getAvailableHeight = (): number => {
  return SCREEN_HEIGHT - STATUS_BAR_HEIGHT - (isNotchScreen ? BOTTOM_SAFE_HEIGHT : 0);
};

// 获取设备像素比
export const getPixelRatio = (): number => {
  return pixelRatio;
};

// 监听屏幕尺寸变化
export const addDimensionsListener = (callback: (dimensions: { window: ScaledSize; screen: ScaledSize }) => void) => {
  return Dimensions.addEventListener('change', callback);
};

// 移除屏幕尺寸变化监听
export const removeDimensionsListener = (subscription: { remove: () => void }) => {
  subscription.remove();
};

// 导出所有工具函数
export default {
  adapt,
  adaptFontSize,
  adaptHeight,
  adaptMargin,
  adaptRadius,
  getScreenWidth,
  getScreenHeight,
  getStatusBarHeight,
  getBottomSafeHeight,
  getNavBarHeight,
  getAvailableHeight,
  getPixelRatio,
  addDimensionsListener,
  removeDimensionsListener,
  DeviceType,
  ScreenSize,
};
